********Figure M.1 - Indonesia********

*Load data and create differences
cd "$reploc/data/created_data"
use indo_full, clear

sort pidlink ifls
drop if ifls==.
keep if panel==1

keep rbuc*  gambleaverse ifls pidlink inflation imp_age panel biryear_impl grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? birprov_impl industry

reshape wide rbuc*  gambleaverse inflation imp_age panel grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co? grbrprovSD_lam0co? industry, i(pidlink) j(ifls)


local varlist1 rbuc rbucB rbuc_nga rbuc_garefined rbucgaref1a rbucgaref2a rbucgaref3a rbucgaref4a gambleaverse inflation grbrprovAvg_lam0 grbrprovSD_lam0 grbrprovAvg_lam0co2 grbrprovSD_lam0co2 grbrprovAvg_lam0co3 grbrprovSD_lam0co3 industry


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'5-`var'4
}

*Generate cohort by birthyear variables to cluster on
egen coh_prov_clust=group(biryear_impl birprov_impl)

*Regression
local x rbuc_garefined
local rhsvari oldindoonly
local rhsvaruse _lam0co3dif
local usei grbr
local ind industry4

drop if `x'dif==.

reg `x'dif `usei'provAvg`rhsvaruse' `usei'provSD`rhsvaruse'  inflationdif c.`usei'provAvg`rhsvaruse'#ib3.`ind' c.`usei'provSD`rhsvaruse'#ib3.`ind', vce(cluster coh_prov_clust)

cd "$reploc/results"	
regsave using indo_industry.dta, tstat pval ci level(90) replace

*Graphs

cd "$reploc/results"	
use indo_industry.dta, clear

local measures Avg SD

foreach measu in `measures' {

preserve

local is 1 2 4 5 6 7 8 9

gen varinterest=0
foreach i in `is'{
replace varinterest=1 if var=="`i'.industry4#c.grbrprovAvg_lam0co3dif" | var=="`i'.industry2#c.grbrprovAvg_lam0co3dif"
replace varinterest=2 if var=="`i'.industry4#c.grbrprovSD_lam0co3dif" | var=="`i'.industry2#c.grbrprovSD_lam0co3dif"
}

gen locat=.
replace locat=1 if var=="1.industry4#c.grbrprovAvg_lam0co3dif" | var=="1.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=3 if var=="1.industry4#c.grbrprovSD_lam0co3dif" | var=="1.industry2#c.grbrprovSD_lam0co3dif"
replace locat=6 if var=="2.industry4#c.grbrprovAvg_lam0co3dif" | var=="2.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=8 if var=="2.industry4#c.grbrprovSD_lam0co3dif" | var=="2.industry2#c.grbrprovSD_lam0co3dif"
replace locat=11 if var=="4.industry4#c.grbrprovAvg_lam0co3dif" | var=="4.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=13 if var=="4.industry4#c.grbrprovSD_lam0co3dif" | var=="4.industry2#c.grbrprovSD_lam0co3dif"
replace locat=16 if var=="5.industry4#c.grbrprovAvg_lam0co3dif" | var=="5.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=18 if var=="5.industry4#c.grbrprovSD_lam0co3dif" | var=="5.industry2#c.grbrprovSD_lam0co3dif"
replace locat=21 if var=="6.industry4#c.grbrprovAvg_lam0co3dif" | var=="6.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=23 if var=="6.industry4#c.grbrprovSD_lam0co3dif" | var=="6.industry2#c.grbrprovSD_lam0co3dif"
replace locat=26 if var=="7.industry4#c.grbrprovAvg_lam0co3dif" | var=="7.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=28 if var=="7.industry4#c.grbrprovSD_lam0co3dif" | var=="7.industry2#c.grbrprovSD_lam0co3dif"
replace locat=31 if var=="8.industry4#c.grbrprovAvg_lam0co3dif" | var=="8.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=33 if var=="8.industry4#c.grbrprovSD_lam0co3dif" | var=="8.industry2#c.grbrprovSD_lam0co3dif"
replace locat=36 if var=="9.industry4#c.grbrprovAvg_lam0co3dif" | var=="9.industry2#c.grbrprovAvg_lam0co3dif"
replace locat=38 if var=="9.industry4#c.grbrprovSD_lam0co3dif" | var=="9.industry2#c.grbrprovSD_lam0co3dif"

capture label drop locatlab
label define locatlab 2 "Agriculture" 7 "Mining" 12 "Utilities" 17 "Construction" 22 "Wholesale & Retail" 27 "Transportation" 32 "Finance" 37 "Social Services" 

local tit "Heterogeneity of Main Resuls by Industry"
local numpl 8
label values locat locatlab

if "`measu'"=="Avg" {
local imi=1
local col "maroon"
}


if "`measu'"=="SD" {
local imi=2
local col "gold"
}

twoway (scatter locat coef if varinterest==`imi', mcolor(`col')) (rcap ci_lower ci_upper locat if varinterest==`imi', lcolor(`col') horizontal) ///
, legend(off) graphregion(color(white)) xline(0) ylabel(2 7 12 17 22 27 32 37, valuelabel labsize(vsmall) noticks angle(horizontal)) ///
ytitle("Industry (Baseline Category: Manufacturing)", size(vsmall))

graph export FigureM1Indonesia`measu'.eps, replace



restore

}
